package com.lill.org.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.transaction.CommonTransaction;
import com.lill.common.component.transaction.TM;
import com.lill.common.component.util.IdUtil;
import com.lill.org.constant.ArchitectureType;
import com.lill.org.dao.ICompanyArchitectureDao;
import com.lill.org.dao.ICompanyBranchDao;
import com.lill.org.model.CompanyArchitectureModel;
import com.lill.org.model.CompanyBranchModel;
import com.lill.org.model.EmployeeModel;
import com.lill.org.service.ICompanyBranchService;
import com.lill.org.service.IEmployeeService;

/**
 * 分公司管理Service实现类
 *
 * @author lill
 * @date 2019-04-24 15:25:17
 */
@Service("com.lill.org.service.impl.CompanyBranchServiceImpl")
public class CompanyBranchServiceImpl implements ICompanyBranchService {
    @Autowired
    private ICompanyBranchDao companyBranchDao;
    @Autowired
    private ICompanyArchitectureDao architectureDao;
	@Autowired
	private IEmployeeService employeeService;

    public CompanyBranchModel queryObject(String id) {
    	CompanyBranchModel branchModel=companyBranchDao.queryObject(id);
        return branchModel;
    }

	@Override
	public CompanyBranchModel queryObjectAllName(String id) {
		return companyBranchDao.queryObject(id);
	}

	public List<CompanyBranchModel> queryList(Map<String, Object> map) {
        return companyBranchDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return companyBranchDao.queryTotal(map);
    }

    public int save(CompanyBranchModel orgCompanyBranch) {
    	String id=IdUtil.createIdbyUUID();
    	orgCompanyBranch.setId(id);
    	
    	CompanyArchitectureModel parentArchitecture=architectureDao.queryObject(orgCompanyBranch.getCompanyId());
    	
    	CompanyArchitectureModel architecture=new CompanyArchitectureModel();
    	architecture.setId(id);
    	architecture.setParentId(parentArchitecture.getId());
    	if(parentArchitecture.getParentList().contains(parentArchitecture.getId())){
    		architecture.setParentList(parentArchitecture.getParentList());
    	}else{
    		architecture.setParentList(parentArchitecture.getParentList()+","+parentArchitecture.getId());
    	}
    	architecture.setType(ArchitectureType.BRANCH.getVal());
    	architecture.setNum(0);
    	architectureDao.save(architecture);
    	
        return companyBranchDao.save(orgCompanyBranch);
    }
    public int save(CompanyBranchModel orgCompanyBranch,String id ) {
    	orgCompanyBranch.setId(id);

    	CompanyArchitectureModel parentArchitecture=architectureDao.queryObject(orgCompanyBranch.getCompanyId());

    	CompanyArchitectureModel architecture=new CompanyArchitectureModel();
    	architecture.setId(id);
    	architecture.setParentId(parentArchitecture.getId());
    	if(parentArchitecture.getParentList().contains(parentArchitecture.getId())){
    		architecture.setParentList(parentArchitecture.getParentList());
    	}else{
    		architecture.setParentList(parentArchitecture.getParentList()+","+parentArchitecture.getId());
    	}
    	architecture.setType(ArchitectureType.BRANCH.getVal());
    	architecture.setNum(0);
    	architectureDao.save(architecture);

		EmployeeModel employee=new EmployeeModel();
		employee.setCompanyId(orgCompanyBranch.getCompanyId());
		employee.setDepartId(orgCompanyBranch.getId());
		if(StringUtils.isEmpty(orgCompanyBranch.getPersonId())){
			employee.setName(orgCompanyBranch.getAttn());
			employee.setPosition(orgCompanyBranch.getPosition());
			employee.setPhone(orgCompanyBranch.getPhone());
		}else{
			employee.setPersonId(orgCompanyBranch.getPersonId());
		}
		employee.setCreateTime(orgCompanyBranch.getCreateTime());
		employee.setCreateUser(orgCompanyBranch.getCreateUser());
		employee.setStatus(1);

		employeeService.saveBackModel(employee);

        return companyBranchDao.save(orgCompanyBranch);
    }

    @CommonTransaction({TM.org})
    public int update(CompanyBranchModel orgCompanyBranch) {
		if(orgCompanyBranch.getParentId() != null){
			CompanyArchitectureModel oldArchitecture=architectureDao.queryObject(orgCompanyBranch.getId());
			if(!oldArchitecture.getParentId().equals(orgCompanyBranch.getParentId())){
				CompanyArchitectureModel parentArchitecture=architectureDao.queryObject(orgCompanyBranch.getParentId());

				CompanyArchitectureModel updateArchitecture=new CompanyArchitectureModel();
				updateArchitecture.setId(orgCompanyBranch.getId());
				updateArchitecture.setParentId(orgCompanyBranch.getParentId());
				if(parentArchitecture.getParentList().contains(parentArchitecture.getId())){
					updateArchitecture.setParentList(parentArchitecture.getParentList());
				}else{
					updateArchitecture.setParentList(parentArchitecture.getParentList()+","+parentArchitecture.getId());
				}
				architectureDao.update(updateArchitecture);

				/*******子集处理删除旧的父级*******/
				String oldParents=oldArchitecture.getParentList();
				if(StringUtils.isNotEmpty(oldParents)){
					String[] oldParentList=oldParents.split(",");
					Map<String, Object> oldDelMap=new HashMap<String, Object>();
					oldDelMap.put("id", orgCompanyBranch.getId());
					for (String oldParentId : oldParentList) {
						/*********利用replace 将子集的父级合替换为空**********/
						oldDelMap.put("parentOldId", oldParentId);
						architectureDao.updateDeleteOldParent(oldDelMap);
					}
				}
				String newParents=updateArchitecture.getParentList();
				if(StringUtils.isNotEmpty(newParents)){
					/*********利用CONCAT 将子集的父级合连接父级**********/
					Map<String, Object> newAddMap=new HashMap<String, Object>();
					newAddMap.put("id", orgCompanyBranch.getId());
					newAddMap.put("parentNewIds", newParents+",");
					architectureDao.updateAddOldParent(newAddMap);
				}
//    		Map<String, Object> updateChildMap=new HashMap<String, Object>();
//    		if(updateArchitecture.getParentList().contains(updateArchitecture.getId())){
//    			updateChildMap.put("udapteParents", updateArchitecture.getParentList());
//    		}else{
//    			updateChildMap.put("udapteParents", updateArchitecture.getParentList()+","+updateArchitecture.getId());
//    		}
//    		if(oldArchitecture.getParentList().contains(oldArchitecture.getId())){
//    			updateChildMap.put("oldParents", oldArchitecture.getParentList());
//    		}else{
//    			updateChildMap.put("oldParents", oldArchitecture.getParentList()+","+oldArchitecture.getId());
//    		}
//    		architectureDao.updateChildParentList(updateChildMap);

				Map<String, Object> mapParam=new HashMap<String, Object>();
				mapParam.put("ids", oldArchitecture.getParentList().split(","));
				mapParam.put("num", -1*oldArchitecture.getNum());
				architectureDao.updatePersonNum(mapParam);

				mapParam.put("ids", updateArchitecture.getParentList().split(","));
				mapParam.put("num", oldArchitecture.getNum());
				architectureDao.updatePersonNum(mapParam);

			}
		}
        return companyBranchDao.update(orgCompanyBranch);
    }

    public int delete(String id) {
        return companyBranchDao.delete(id);
    }

    public int deleteBatch(String[] ids) {
        return companyBranchDao.deleteBatch(ids);
    }
    
	public int deleteLogicByMap(Map<String, Object> map) {
		return companyBranchDao.deleteLogicByMap(map);
	}

	@Override
	public Map<String, Object> queryUserFirstBranch(String id) {
		return companyBranchDao.queryUserFirstBranch(id);
	}

	@Override
	public List<CompanyBranchModel> queryLists(Map<String, Object> map) {
		return companyBranchDao.queryLists(map);
	}

	@Override
	public int queryTotals(Map<String, Object> map) {
		return companyBranchDao.queryTotals(map);
	}

	@Override
	public Integer businessChange(Map<String, Object> params) {
		return companyBranchDao.businessChange(params);
	}

	@Override
	public List<CompanyBranchModel> queryUserBranch(Map<String, Object> map) {
		return companyBranchDao.queryUserBranch(map);
	}
}
